<?php
/**
 * text
 *
 * script
 *
 * @version:	0.1a
 * @package:	global
 * @author:	alex_ez
 * @created:	27.01.2009 01:52:13
 *
 * @copyright:	(c) 2009 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */


class static_admin_controller extends admin_controller
{
	static public function get_menu_items( )
	{
		return array(
			'static' => array( 'text' => __('Static pages'), 'section' => 'content', 'group' => __('Content data') )
			);
	}
	
	public function on_start( )
	{
		$this ->visitor = visitor::instance( );
		$this ->visitor ->need_auth( 'admin' );
		$this ->frame = 'admin/!index.tpl';
		$this ->model = new staticdata( );
	}
	
	
	public function page_index( $p, $o )
	{
		$this ->title = 'Статичные страницы';
		
		$r = array( 'list' => $this ->model ->by( array( ), 'tag+' ) );
		
		return $r;
	}

	public function page_add( $p, $o )
	{
		$form = new Form( $this, 'admin/static/add' );
		$form ->set_option( 'name', 'addpage' );
		$form ->set_option( 'action', '/ajax/admin/static/add/' );
		
		$form ->set_data( request::instance( ) ->g( array( 'tag', 'title' ) ) );
		
		$r['fields'] = $form ->get_field_set( );
		$r['options'] = $form ->get_options( );
		$r['template'] = 'admin/__add.tpl';
		$r['title'] = texo::translate('Add static page');
		
		bc::log( $r ); //"{'action':'/ajax/info/cashless/add/','method':'POST','name':'addoffer'}";
		
		return $r;
	}
	
	public function page_edit( $p, $o )
	{
		$this ->pageTitle = 'edit';
		
		$id = (int)(Request::instance( ) ->g_id);
		
		$p = $this ->model ->get( $id );
		if( !$p ) Response::instance( ) ->notfound( );
		
		$form = new Form( $this, 'admin/static/add' );
		$form ->set_option( 'name', 'editpage' );
		$form ->set_option( 'action', '/ajax/admin/static/edit/?id='.$id );
		
		$p['meta'] = (array)(json::decode( @$p['meta'] ));
		$form ->set_data( $p );
		
		$r['fields'] = $form ->get_field_set( );
		$r['options'] = $form ->get_options( );
		$r['template'] = 'admin/__add.tpl';
		
		bc::log( $r ); //"{'action':'/ajax/info/cashless/add/','method':'POST','name':'addoffer'}";
		
		return $r;
	}
	
	public function ajax_add( $z )
	{
		$form = new Form( $this, 'admin/static/add' );
		
		// fetch post data if data is bogus return error
		try { $p = $form ->get_post_data( ); } catch( form_exception $e ) { return array( 'error' => $e ->getMessage() ); }
		$p['meta'] = json::encode( array_filter( (array)(@$p['meta']) ) );
		
		$id = $this ->model ->add( $p );
		if( $id<=0 ) return array( 'error' => 'Путь не добавлен. Внутренняя ошибка.' );
		
		return array( 'message' => 'Путь добавлен. id: '.$id, 'location' => '/admin/static/' );
	}

	public function ajax_edit( $z )
	{
		$form = new Form( $this, 'admin/static/add' );
		// fetch post data if data is bogus return error
		try { $p = $form ->get_post_data( ); } catch( form_exception $e ) { return array( 'error' => $e ->getMessage() ); }
		$p['meta'] = json::encode( array_filter( (array)(@$p['meta']) ) );
		
		$id = (int)(Request::instance( ) ->g_id);
		if( $id<=0 ) return array( 'error' => 'Страница не существует' );
		
		$eo = $this ->model ->edit( $id, $p );
		if( $eo!=1 ) return array( 'error' => 'Страница не отредактировалась' );
		//bc::log( $p, 'newdata' );
		//return array( 'error' => $eo );
		
		return array( 'message' => 'Страница отредактирована', 'location' => '/admin/static/' );
	}
	


	public function exists( $tag, $type = 'page' )
	{
		if( strpos( 'page,block', $type ) === false )
			$type == 'page';

		$prefix = ( $type == 'page' ) ? '' : '$';
		$tag = trim($prefix . $tag, '/');

		return !!( $this->model->count( array( 'tag' => $prefix.$tag ) ) );
	}
	
	// shamanstvo
	public function page_needtag( )
	{
		$request = request::instance( );
		$p = $request ->g( array( 'tag', 'title' ) );
		if( empty( $p['tag'] ) ) response::instance( ) ->notfound( );
		
		$item = $this ->model ->by_tag( $p['tag'] );
		
		$url = isset( $item['id'] )
			? sprintf( '/admin/static/edit/?id=%d', $item['id'] )
			: sprintf( '/admin/static/add/?%s', http_build_query( $p ) );
		
		response::instance( ) ->seeother( $url );
	}
	
	
	public function page_remove( $p, $o )
	{
		$this ->pageTitle = 'remove';
		
		$id = (int)(Request::instance( ) ->g_id);
		
		$this ->model ->remove( $id );
		
		response::instance()->moved( '/admin/static/' );
	}
	
	
	private $field_sets = array(
'admin/static/add' =>
"{'head':	{type:'header',	label:'Страница'},
'tag':		{type:'string',	label:'Путь',required:true,class:'maxLength:128'},
'title':	{type:'string',	label:'Название',required:true,class:'maxLength:80'},
'body':		{type:'text',	label:'Содержание',required:true},

'headmeta':	{type:'header',	label:'Мета'},
'meta[title]':		{type:'string',	label:'Title'},
'meta[keywords]':	{type:'string',	label:'Keywords'},
'meta[description]':	{type:'text',	label:'Description'}}",
);

	public function get_field_set( $url = null, $decode = false )
	{
		$iurl = is_null( $url ) ? $this ->getInnerUrl( ) : $url;
		$r = isset( $this ->field_sets[ $iurl ] ) ? $this ->field_sets[ $iurl ] : ''; //json::decode( $fields );
		
		$replacements = array(
			'now' => date('d.m.Y'),
			'yesterday' => date('d.m.Y',time()-86400),
			'tomorrow' => date('d.m.Y',time()+86400),
			'user.login' => visitor::instance() ->user['login'],
			);
		
		// replacing
		$r = str_replace(
			array_map( create_function('$v','return "{{$v}}";'), array_keys( $replacements ) ),
			array_values( $replacements ),
			$r );
		
		return $decode ? json::decode( $r ) : $r;
	}
	
}

?>